package com.adee.java.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class SelectorClientTest {
    public static void main(String[] args) throws IOException, InterruptedException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.setOption(StandardSocketOptions.SO_SNDBUF, 16);
        socketChannel.configureBlocking(false);
        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8000);
        // 如果SocketChannel处于非阻塞模式并且你调用了connect()方法的话，该方法会在一个链接建立之前就返回。
        // 你可以调用finishConnect()方法去判断一个链接有没有建立
        if(!socketChannel.connect(address)) { // 连接未成功，立即返回
            while (!socketChannel.finishConnect()) { // 循环检查连接是否完成
                System.out.println("连接尚未完成...");
                Thread.sleep(10);
            }
        }
        // 连接成功，发送数据
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()) {
            String str = scanner.nextLine();
            byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
            ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
            buffer.put(bytes); // 写buffer
            buffer.flip();
            int receiveBufferSize = socketChannel.socket().getReceiveBufferSize();
            int sendBufferSize = socketChannel.socket().getSendBufferSize();
            System.out.printf("receiveBufferSize: %d\n", receiveBufferSize);
            System.out.printf("sendBufferSize: %d\n", sendBufferSize);
            System.out.printf("buffer可发送字节数：%d\n", buffer.limit()-buffer.position());

            ByteBuffer mBuffer = ByteBuffer.wrap(megaBytes(10, (byte) 121, true));
            while (mBuffer.hasRemaining()) { // 没发送完继续发送
                socketChannel.write(mBuffer); // 读buffer，写入socketChannel
            }

            receiveBufferSize = socketChannel.socket().getReceiveBufferSize();
            sendBufferSize = socketChannel.socket().getSendBufferSize();
            System.out.printf("receiveBufferSize: %d\n", receiveBufferSize);
            System.out.printf("sendBufferSize: %d\n", sendBufferSize);
            buffer.clear();
            socketChannel.read(buffer);
            System.out.println(new String(buffer.array()));
        }

    }

    // 创建一个n兆大小的字节数组，初始化字节为b，是否自定义初始化字节
    private static byte[] megaBytes(int n, byte b, boolean init) {
        byte[] k = new byte[1024];
        if(init) { // 初始化为指定字节b
            for (int i = 0; i < k.length; i++) {
                k[i] = b;
            }
        }
        byte[] m = new byte[1024*1024];
        for (int i = 0; i < 1024; i++) {
            System.arraycopy(k, 0, m, i*1024, 1024);
        }
        byte[] ret = new byte[n*1024*1024];
        for (int i = 0; i < n; i++) {
            System.arraycopy(m, 0, ret, i*1024*1024, 1024*1024);
        }
        return ret;
    }
}
